import request from "../utils/request"

export default {
  /**
   * 注册
   * @param {string} email 邮箱
   * @param {string} password 密码
   * @returns Promise
   */
  register(email, password, username) {
    return request.post("/registration", {
      user: {
        email,
        password,
        username
      }
    })
  },

  /**
   * 登录
   * @param {String} email 邮箱
   * @param {String} password 密码
   * @returns Promise
   */
  login(email, password) {
    return request.post("/login", {
      user: {
        email,
        password
      }
    })
  },

  /**
   *
   * @param {String} username 用户名
   * @param {String} bio 个人介绍
   * @returns Promise
   */
  updateUserInfo(username, bio) {
    return request.post("/updateUserInfo", {
      user: {
        username,
        bio
      }
    })
  },

  /**
   * @returns Promise
   */
  getUserInfo() {
    return request({
      url: "/getUserInfo",
      method: "POST"
    })
  },

  /**
   * 添加或保存收藏
   * @param {*} data
   * @returns
   */
  saveCollect({ _id: webId, webSite, webName, webIco, inDrawer }) {
    if (typeof webIco !== "string") {
      webIco = ""
    }
    return request.post("/collect", {
      webId,
      webSite,
      webName,
      webIco,
      inDrawer
    })
  },

  /**
   * 获取收藏列表
   * @param {*} data
   * @returns
   */
  getCollects(data) {
    return request.get("/listCollection", data)
  },
  /**
   * 删除收藏
   * @param {String} id
   * @returns
   */
  delCollect(id) {
    return request.delete("/delCollect", {
      webId: id
    })
  },

  /**
   * 移入到抽屉
   * @param {*} id
   * @returns
   */
  moveToDrawerOrDesk(id, inDrawer) {
    return request.post("/dargTodrawer", {
      webId: id,
      inDrawer: inDrawer
    })
  },

  /**
   * 获取网站的favicon
   * @param {*} url
   * @returns
   */
  getWebIconAnySize(url) {
    return request.get("/getWebIconAnySize", {
      webSite: url
    })
  },

  /**
   * 拖拽排序之后更新数据
   * @param {*} orders
   * @returns
   */
  updateCollectOrder(orders) {
    return request.post("/updateCollectOrder", orders)
  },
  uploadWallpaper(file) {
    const formData = new FormData()
    formData.append("Wallpaper", file)
    return request.post("/uploadWallpaper", formData, {
      headers: {
        "Content-Type": "multipart/form-data;charset=UTF-8"
      }
    })
  },
  // 获取所有todo列表
  getTodoList() {
    return request.get("/getTodoList")
  },
  // 添加todo
  createTodo(title) {
    return request.post("/createTodo", { title })
  },
  // 更新todo状态
  updateTodo(todoId, done) {
    return request.post("/updateTodo", {
      todoId,
      complete: done
    })
  },
  // 删除todo
  deleteTodo(todoId, allTrue) {
    return request.delete("/deleteTodo", {
      todoId,
      allTrue
    })
  },
  uploadFiles(file) {
    const formData = new FormData()
    formData.append("userImg", file)
    return request.post("/uploadUserImg", formData, {
      headers: {
        "Content-Type": "multipart/form-data;charset=UTF-8"
      }
    })
  }
}
